// const { request } = require("../../requests");
import regeneratorRuntime from '../../lib/runtime/runtime';
import { request } from "../../requests/index.js";

// pages/goods_detail/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsDetail:{}
  },
  goodsInfo:{},

  params:{
    goods_id:0
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    const gid = options.goods_id;
    this.params.goods_id = gid;
    this.getGoodsDetail();

  },

  async getGoodsDetail(){
    const res = await request({url:"/goods/detail",data:this.params})
    this.goodsInfo = res;
    this.setData({
      goodsDetail:{
        goods_name :res.goods_name,
        goods_price:res.goods_price,
        goods_introduce:res.goods_introduce.replace(/\.webp/g,".jpg"),
        pics:res.pics
      }
    })
    console.log(this.data.goodsDetail);
  },
  handleSwiperItemTap(e){
    let urls = this.goodsInfo.pics.map(v=>v.pics_mid)
    const current=e.currentTarget.dataset.goodsurl;
    wx.previewImage({
      current,
      urls
    });
      
  },

  handleAddCart(){
    let cart = wx.getStorageSync("cart")||[];
    let index = cart.findIndex(v=>v.goods_id===this.goodsInfo.goods_id)
    if(index===-1){
      this.goodsInfo.num=1;
      this.goodsInfo.checked=true
      cart.push(this.goodsInfo);
    }else{
      cart[index].num++;
    }
    wx.setStorageSync("cart", cart);
    wx.showToast({
      title: '加入成功',
      icon: 'success',
      mask: true,
      
    });
      
      
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})